﻿#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QObject>
#include <QVariant>
#include <QColor>
#include <QQmlContext>
#include <QTimer>

#include "bluetoothCtrl.h"
#include "QtKLog/QtKLog.h"
#include "BluetoothDeviceModel.h"
#include "LowEnergyInfoModel.h"

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    // 注册C++类型
    qmlRegisterType<BluetoothDeviceModel>("com.mycompany.customType", 1, 0, "BluetoothDeviceModel");
    qmlRegisterType<LowEnergyInfoModel>("com.mycompany.customType", 1, 0, "LowEnergyInfoModel");
    //qmlRegisterType<BluetoothCtrl>("com.mycompany.customType", 1, 0, "BluetoothCtrl");
    // 注册C++类型Singleton
    qmlRegisterSingletonType<QtKLog>("com.mycompany.customType", 1, 0, "QtKLogSingleton", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
        Q_UNUSED(engine)
        Q_UNUSED(scriptEngine)

        QtKLog * singleton = new QtKLog();
        // 安装消息处理
        //singleton->CreateLogFile();
        singleton->SetSendLogSignal(true);
        singleton->SetSingle(true);
        //singleton->InstallMessageHandler();
        //qInstallMessageHandler(QtKLog::KMessageLogOutput);
        //qInfo() << "software start";

        return singleton;
    });
    // 注册C++类型Singleton
    qmlRegisterSingletonType<BluetoothCtrl>("com.mycompany.customType", 1, 0, "BluetoothCtrlSingleton", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
        Q_UNUSED(engine)
        Q_UNUSED(scriptEngine)

        BluetoothCtrl * singleton = BluetoothCtrl::singleton();//new BluetoothCtrl();
        return singleton;
    });

#if 0
    // 注册C++类型Singleton
    qmlRegisterSingletonType<BluetoothDeviceModel>("com.mycompany.customType", 1, 0, "BluetoothDeviceModelSingleton", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
        Q_UNUSED(engine)
        Q_UNUSED(scriptEngine)

        BluetoothDeviceModel * model = BluetoothDeviceModel::Singleton();//new BluetoothDeviceModel();
        BluetoothCtrl::Singleton()->SetBluetoothDeviceModel(model);
        static QTimer timer2;
        QObject::connect(&timer2, &QTimer::timeout, [](){
            BluetoothDeviceModel::Singleton()->add("未知", 2, "{00001101-0000-1000-1112-234235325}", "HC-05" , "20:00:15:25::65:87");
            qDebug() << "model->add();";

        });
        timer2.start(2000);

        return model;
    });
#endif

    {

    }


    QQmlApplicationEngine engine;

#if 0
    // 设置rootContext的属性，然后可以直接在qml中使用，C++中也可以使用
    BluetoothDeviceModel *model = new BluetoothDeviceModel();
    engine.rootContext()->setContextProperty("bluetoothDeviceModel", model);
    QTimer timer;
    QObject::connect(&timer, &QTimer::timeout, [&](){
        model->add("未知", 2, "{00001101-0000-1000-8000-234235325}", "HC-05" , "20:00:15:25::65:87");
        qDebug() << "model->add();";

    });
    timer.start(2000);
#endif

    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app,
                     [url](QObject *obj, const QUrl &objUrl)
    {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

#if 1
    // 通过qml的对象的objectName属性查找到对象，然后可以设置属性和通过QMetaObject::invokeMethod调用部分函数，或者直接转化数据调用
    QList<QObject*> rootObjects = engine.rootObjects();
    if(!rootObjects.empty())
    {
        QObject* rootObject = rootObjects.at(0);
        qDebug() << "rootObject:" << rootObject;
        // 查找list模型
        QObject* objBluetoothDeviceModel = rootObject->findChild<QObject*>("bluetoothDeviceModel");
        qDebug() << "objBluetoothDeviceModel:" << objBluetoothDeviceModel;
        if(objBluetoothDeviceModel)
        {
            BluetoothDeviceModel *model = qobject_cast<BluetoothDeviceModel*>(objBluetoothDeviceModel);
            BluetoothCtrl::singleton()->setBluetoothDeviceModel(model);
            //static QTimer timer1;
            //QObject::connect(&timer1, &QTimer::timeout, [&](){
            //    model->addService("未知", 2, "{00001101-0000-1000-8000-234235325}", "HC-05" , "20:00:15:25::65:87");
            //    qDebug() << "model->add();";
            //});
            //timer1.start(2000);
        }
        else
        {
            return -3;
        }
        // 查找list模型
        QObject* objLowEnergyInfoModel = rootObject->findChild<QObject*>("lowEnergyInfoModel");
        qDebug() << "objLowEnergyInfoModel:" << objLowEnergyInfoModel;
        if(objLowEnergyInfoModel)
        {
            LowEnergyInfoModel *model = qobject_cast<LowEnergyInfoModel*>(objLowEnergyInfoModel);
            BluetoothCtrl::singleton()->setLowEnergyInfoModel(model);
            //static QTimer timer1;
            //QObject::connect(&timer1, &QTimer::timeout, [&](){
            //    model->addService("未知", 2, "{00001101-0000-1000-8000-234235325}", "HC-05" , "20:00:15:25::65:87");
            //    qDebug() << "model->add();";
            //});
            //timer1.start(2000);
        }
        else
        {
            return -3;
        }
#if 0
        if(0 && obj)
        {
            obj->setProperty("color", QColor::fromRgb(255, 0, 0));
            bool bRet = QMetaObject::invokeMethod(obj, "doLayout");
            qDebug() << "call doLayout return - " << bRet;
        }
#endif
    }
    else
    {
        return -2;
    }
#endif

    return app.exec();
}
